//
//  UIView+NEDAnimation.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/10/15.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "UIView+NEDAnimation.h"
#import "UIColor+NEDStyle.h"

@implementation UIView (NEDAnimation)


/** 点赞动画*/
- (void)ned_ani_zan{
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation.values = @[@1.2, @1.0];
    animation.duration = 0.3;
    animation.calculationMode = kCAAnimationCubic;
    [self.layer addAnimation:animation forKey:@"transform.scale"];
}

/** 大小跳动 动画*/
- (void)ned_ani_scale_loop{
    
    [self ned_ani_scale:0.4 values:@[@1.0,@1.2] repeatCount:HUGE_VALF];
}

/** 大小跳动 动画*/
- (void)ned_ani_scale_once{
    
    [self ned_ani_scale:0.5 values:@[@1,@1.2,@1,@1.1,@1] repeatCount:1];
}

- (void)ned_ani_scale:(NSTimeInterval)time values:(NSArray *)values repeatCount:(NSInteger)repeatCount{
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = values;
    animation.duration = time;
    animation.autoreverses = NO; //是否重复
    animation.repeatCount = repeatCount;//次数
    animation.calculationMode = kCAAnimationCubic;
    animation.removedOnCompletion = NO;//切换界面的时候会继续播放
    [self.layer addAnimation:animation forKey:@"transform.scale.loop"];
}

/** 旋转 动画*/
- (void)ned_ani_rotate_loop{
    [self ned_ani_rotate_loop_duation:4];
}

- (void)ned_ani_rotate_loop_duation:(NSTimeInterval)duration{
    
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    rotation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    rotation.duration = duration;
    rotation.repeatCount = HUGE_VALF;
//    rotation.autoreverses = YES;
    [self.layer addAnimation:rotation forKey:@"transform.rotation.z.loop"];
}

- (void)ned_ani_rotateReverse_loop_duation:(NSTimeInterval)duration{
    
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    rotation.toValue = [NSNumber numberWithFloat:-2 * M_PI];
    rotation.duration = duration;
    rotation.repeatCount = HUGE_VALF;
//    rotation.autoreverses = YES;
    [self.layer addAnimation:rotation forKey:@"transform.rotation.z.loop"];
}


/** 旋转 动画 一次*/
- (void)ned_ani_rotate_once{
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    rotation.toValue = [NSNumber numberWithFloat:M_PI];
    rotation.duration = 1;
    rotation.repeatCount = 1;
//    rotation.autoreverses = YES;
    [self.layer addAnimation:rotation forKey:@"transform.rotation.z.loop"];
}
- (void)ned_ani_rotate_top_bottom:(BOOL)istop{
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    rotation.fromValue = [NSNumber numberWithFloat:istop?0.0:M_PI]; // 起始角度
    rotation.toValue = [NSNumber numberWithFloat:istop?M_PI:0.0];
    rotation.duration = 0.3;
    rotation.repeatCount = 1;
    rotation.removedOnCompletion = NO;
    rotation.fillMode = kCAFillModeForwards;
//    rotation.autoreverses = YES;
    [self.layer addAnimation:rotation forKey:@"transform.rotation.z.loop"];
}


/** 点击缩放动画*/
- (void)ned_ani_clickScaling{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@1.0,@1.2,@0.9,@1.05,@0.98,@1];
    animation.duration = 0.8;
    animation.repeatCount = 1;
    animation.calculationMode = kCAAnimationCubic;
    [self.layer addAnimation:animation forKey:nil];
}

/** 左右摆动动画*/
- (void)ned_waveMotionAnnimation:(double)angle duration:(NSTimeInterval)time repeatCount:(NSInteger)count{
    //初始化一个动画
    CABasicAnimation *baseAnimation = [CABasicAnimation animation];
    //动画运动的方式，现在指定的是围绕Z轴旋转
    baseAnimation.keyPath = @"transform.rotation.z";
    //动画持续时间
    baseAnimation.duration = time;
    //开始的角度
    baseAnimation.fromValue = [NSNumber numberWithFloat:angle];
    //结束的角度
    baseAnimation.toValue = [NSNumber numberWithFloat:-angle];
    //动画的运动方式
    baseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //是否反向移动动画
    baseAnimation.autoreverses = YES;
    baseAnimation.repeatCount = count;
    //动画结束后的状态
    baseAnimation.fillMode = kCAFillModeForwards;

    [self.layer addAnimation:baseAnimation forKey:@"transform.rotation.wave.loop"];
    
    
}

/** 沿y轴旋转 */
- (void)ned_rotationBaseY:(NSTimeInterval)duration repeatCount:(NSInteger)count {
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    rotation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    rotation.duration = duration;
    rotation.repeatCount = HUGE_VALF;
//    rotation.autoreverses = YES;
    [self.layer addAnimation:rotation forKey:@"transform.rotation.y.loop"];
}


/** 左右抖动*/
- (void)ned_shake_zuoyou{
    CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
    shakeAnim.keyPath = @"transform.translation.x";
    shakeAnim.duration = 0.2;
    
    CGFloat delta = 2;
    shakeAnim.values = @[@0, @(delta), @(-delta), @0];
    shakeAnim.repeatCount = 3;
    [self.layer addAnimation:shakeAnim forKey:nil];
}

/** 上下抖动*/
- (void)ned_shake_shagnxia{
    
    CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
    shakeAnim.keyPath = @"transform.translation.y";
    shakeAnim.duration = 0.2;
    
    CGFloat delta = 2;
    shakeAnim.values = @[@0, @(delta), @(-delta), @0];
    shakeAnim.repeatCount = 3;
    [self.layer addAnimation:shakeAnim forKey:nil];
}


/** 警示动画*/
- (void)ned_animationWarning{
    
    [self.layer removeAllAnimations];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
    animation.autoreverses = YES;
    animation.duration = 0.1;
    animation.repeatCount = 3;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
    
    [self.layer addAnimation:animation forKey:@"opacity"];
}

/** 亮度变化警示动画*/
- (void)ned_animationLightWarning{
    
    [self.layer removeAllAnimations];
    
    UIColor * bgColor = self.backgroundColor;
    UIColor * bgColor1 = [UIColor ned_transformFromColor:bgColor toColor:[UIColor ned_background] progress:0.1];
    
    CGColorRef bkColor = bgColor.CGColor;
    CGColorRef bkColorLight = bgColor1.CGColor;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];//必须写opacity才行。
    animation.fromValue = (__bridge id)bkColor;
    animation.toValue = (__bridge id)bkColorLight;//这是透明度。
    animation.autoreverses = YES;
    animation.duration = 0.1;
    animation.repeatCount = 3;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
    
    [self.layer addAnimation:animation forKey:@"backgroundColor"];
}

- (void)ned_animationRemoveFromSuperView{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

/** 上下循环跳动 动画*/
- (void)ned_aniPositionVerticalLoop{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.translation.y";
    animation.values = @[@0,@8];
    animation.duration = 0.6;
    animation.autoreverses = YES; //是否重复
    animation.repeatCount = HUGE_VALF;//次数
    animation.calculationMode = kCAAnimationCubic;
    animation.removedOnCompletion = NO;//切换界面的时候会继续播放
    [self.layer addAnimation:animation forKey:@"transform.translation.y.loop"];
}


@end
